<?php
/**
* エラーチェッククラス
**/
require_once( '../system/Class_M_DAO.php' );
class Class_M_INS_SHOP_SHOP extends Class_M_DAO {
	//protected properties begin
	/**
	 *
	 * テーブル名
	 *
	 * @access    protected
	 * @var       string
	 *
	 */
	protected $_table = "M_INS_SHOP_SHOP";
	
	/**
	 *
	 * 主キー名
	 *
	 * @access    protected
	 * @var       string
	 *
	 */
	protected $_pkey = "ins_shop_shop_cd";
	
	/**
	 *
	 * ソート順
	 *
	 * @access    protected
	 * @var       string
	 *
	 */
	 protected $_order = "shop_cd";
	 
	  protected $_tab_last_id = "";
	 
	 protected $_cols     = array(
		"ins_shop_shop_cd" => array(AOKI_VARTYPE_STR),
		"shop_cd"          => array(AOKI_VARTYPE_STR),
		"ins_shop_cd"      => array(AOKI_VARTYPE_STR),
		"disp_ng"          => array(AOKI_VARTYPE_STR)
	 );
	
	 protected $_checks  = array(
		"shop_cd"        => array( "EXIST" ),
		"ins_shop_cd"    => array( "EXIST" )		
	 );
	 
	 //maxlength 
	protected $_maxlens  = array( );
	
	function setTableNameWeek($initid = "" ){
		$this->_tab_last_id = $initid;
		$this->_table       = $this->_table.$initid;
	}
	
	function isExitsTable($intablename ="") {
		if ( empty( $intablename ) ) {
			$tabname = $this->_table;
			$sql = "show tables like '{$tabname}' ";
		} else {
			$sql = "show tables like '{$intablename}' ";
		}
		
		$this->_db->query( $sql );
		if( $this->_db->num_rows() ){
			return true;
		} else {
			return false;
		}
	}	
	function isExitsRow($intablename ="") {
		if ( empty( $intablename ) ) {
			$tabname = $this->_table;
			$sql = "SELECT * FROM {$tabname}  limit 1;  ";
		} else {
			$sql = "SELECT * FROM {$intablename}  limit 1;  ";
		}
		
		$this->_db->query( $sql );
		if( $this->_db->num_rows() ){
			return true;
		} else {
			return false;
		}
	}
	
	 public function search($where ,&$page = NULL, $param =NULL ,$repeat =null,&$copy_matrix_cd=null,&$upd_ins_flg = false){
	 	$fromtablebase = "  INNER JOIN M_SHOP b ON a.shop_cd = b.shop_cd " .
				" INNER JOIN M_INS_SHOP c on a.ins_shop_cd = c.ins_shop_cd ";
		$shopinnerjoin = "";
		if ($param) {
			$shopinnerjoin =" INNER JOIN  (select  aa.shop_cd  from {$this->_table} aa INNER JOIN  M_INS_SHOP cc ON aa.ins_shop_cd = cc.ins_shop_cd ";
			$whereorand = "WHERE";
			if ( isset( $param['ins_shop_name'] ) &&  ( !empty( $param['ins_shop_name'] ) ) ){
				$shopinnerjoin = $shopinnerjoin .  sprintf(" WHERE cc.ins_shop_name like  '%s' ",  "%".$this->esc($param['ins_shop_name']."%") );
				$whereorand = "AND"; 
			}
			if ( isset( $param['ins_shop_cd'] ) &&  ( !empty( $param['ins_shop_cd'] ) ) ){
				$shopinnerjoin = $shopinnerjoin . sprintf(" {$whereorand} cc.ins_shop_cd like  '%s' ",  "%".$this->esc( $param['ins_shop_cd'] )."%" );
			}
			$shopinnerjoin =$shopinnerjoin . ") ins on ins.shop_cd = a.shop_cd ";
		}
		$repeatinnerjoin = "";
		$orderyby = " a.shop_cd  ";
		if ($repeat) {
			$repeatinnerjoin = " INNER JOIN (select ins_shop_cd  from  {$this->_table}  where area_disp_ng = 0  group by  ins_shop_cd having count(*)>1) " .
					" rep ON rep.ins_shop_cd = a.ins_shop_cd  " ;
			$orderyby = " a.ins_shop_cd ";
		}
		
		$fromtable = " FROM {$this->_table}  a ".$fromtablebase.$shopinnerjoin.$repeatinnerjoin;
		
		if ( $copy_matrix_cd ){
			$to_table = "M_INS_SHOP_SHOP".$copy_matrix_cd;
			
			$loginuser = $_SESSION['ADMIN_LOGIN']['STAFF_SEQ'];
			
			$sqlSel = " SELECT a.ins_shop_shop_cd,a.shop_cd,a.ins_shop_cd,a.insert_conums,a.area_pattern_cd,a.area_adnums," .
					"a.area_memo,a.area_disp_ng,a.memo,a.disp_ng," .
					"CURRENT_TIMESTAMP,'{$loginuser}' ".",CURRENT_TIMESTAMP,'{$loginuser}'  " .$fromtable;
			$sqlIns = " insert into {$to_table}(ins_shop_shop_cd,shop_cd,ins_shop_cd,insert_conums,area_pattern_cd," .
					"area_adnums,area_memo,area_disp_ng,memo,disp_ng,u_time,u_user,c_time,c_user) ";
					
			$sqlIns = $sqlIns.$sqlSel;
			if ($where) {
				$thewhere = implode(' AND ', $where);
				$sqlIns     = $sqlIns . " WHERE ".$thewhere ;
			}
			$sqlDel = "delete from  {$to_table}  ";
			$db = $this->_db;
			$this->begin_trans();
			try {
				$copy_matrix_flg = true;
				if (!$db->query( $sqlDel )) {
					$copy_matrix_flg = false;
				}
				if ($copy_matrix_flg) {
					if (!$db->query( $sqlIns )) {
						$copy_matrix_flg = false;
					}
				}
				if ( $copy_matrix_flg ){
					$this->commit_trans();
					$copy_matrix_cd = 'true';
				} else {
					$this->rollback_trans();
					return false;
				}
			} catch (Exception $e) {
					echo "例外キャッチ：", $e->getMessage(), "\n";
					$this->rollback_trans();
					return false;
					
			}
		}
		//結果の販売店情報を更新する
		//update a inner join (select yy from b) c on a.id =b.id  set a.xx = c.yy
		if ($upd_ins_flg) {
			if ($where) {
				$thewhere = implode(' AND ', $where);
				$thewhere = " WHERE ".$thewhere; 
			}
			
			$sql =" update {$this->_table}  a " .$fromtablebase.$shopinnerjoin.$repeatinnerjoin.
				  " set a.insert_conums = c.insert_conums," .
				  " a.area_adnums = c.area_adnums ," .
				  " a.area_pattern_cd = c.area_pattern_cd," .
				  " a.area_memo = c.area_memo" .
				  $thewhere ;
				  
				  if (!$this->_db->query( $sql )) {
						 echo "更新エラー";
					}else {
						$upd_ins_flg = "true";
					}
			}
		$sql = "SELECT a.*, b.shop_name,c.ins_shop_name,(select  user_name FROM M_LOGIN where id  = a.u_user) as m_user_name" .$fromtable;
	
		$sqlcnt = "SELECT count(*) as cnt ".$fromtable;

		return  parent::search($where ,$page ,$sql, $sqlcnt,$orderyby);
	}
	
	/**
	 *
	 *
	 *
	 * @access    public
	 * @param     mixed     $id             主キーの値
	 * @return    mixed     取得に失敗した場合にFALSE, 成功した場合にレコードの内容(array)
	 */
	public function getByShopCd($value) {
	     $fromtable = " FROM {$this->_table}  a  INNER JOIN M_SHOP b ON a.shop_cd = b.shop_cd " .
				" INNER JOIN M_INS_SHOP c on a.ins_shop_cd = c.ins_shop_cd " ;
		$where  =  " WHERE a.shop_cd = '{$value}'" ;
		$sql    = sprintf("SELECT * FROM  {$this->_table}  WHERE {$this->_pkey} = '%s'" , $value );
		
		$sql   = "SELECT a.*, b.shop_name,c.ins_shop_name," .
				 "(select  user_name FROM M_LOGIN where id  = a.u_user) as m_user_name" .$fromtable.$where;
		$this->_db->query( $sql );
		if( $this->_db->num_rows() ){
			$data = array();
			while( $row = $this->_db->fetch() ){
				$data[] = $row;
			}
			return $data;
		}else{
			return  FALSE;
		}
	}
	
	public function getdnpwByShopcd($value,$pattern_cd = NULL){
	 	$sql   = "SELECT a.*,b.ins_shop_name,b.local_name,b.block_name,b.media_name,b.shop_away,b.aoki_compete FROM  {$this->_table} a" .
	 			" INNER JOIN M_INS_SHOP b on a.ins_shop_cd = b.ins_shop_cd" .
	 			" WHERE a.shop_cd = '{$value}' and a.area_disp_ng = '0'" ;
		if ($pattern_cd){
		$sql = $sql. " and a.area_pattern_cd = {$pattern_cd}";
		}
		$this->_db->query( $sql );
		if( $this->_db->num_rows() ){
			$data = array();
			while( $row = $this->_db->fetch() ){
				$data[] = $row;
			}
			return $data;
		}else{
			return  FALSE;
		}
	 }
	 
	public function getShopName($shop_cd){
	 	$sql   = "SELECT shop_name from M_SHOP  WHERE shop_cd = '{$shop_cd}' " ;
	 			
		$this->_db->query( $sql );
		if( $this->_db->num_rows() ){
			if( $row = $this->_db->fetch() ){
				return  $row["shop_name"];
			}
		}else{
			return  "";
		}
	 }
	 
}

?>